﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SFP.Animation2D;

namespace LevelEditor
{
    public partial class LayerPreferencesForm : Form
    {
        LevelEditorForm levelEditorForm;
        private LayerLE layerLE;

        private double zDepth;

        public LayerPreferencesForm(LayerLE layerLE, LevelEditorForm levelEditorForm)
        {
            InitializeComponent();

            this.levelEditorForm = levelEditorForm;
            this.layerLE = layerLE;
            this.zDepth = layerLE.GetLayer().getZDepth();

            nameTextBox.Text = layerLE.GetLayer().getName();
            zDepthTextBox.Text = "" + zDepth;
            scrollableCheckBox.Checked = layerLE.GetLayer().isScrollable();

            levelEditorForm.SetDialogFormOpen(true);
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            layerLE.GetLayerNameLabel().Text = nameTextBox.Text;
            Layer layer = layerLE.GetLayer();
            layer.setName(nameTextBox.Text);
            layer.setZDepth(zDepth);
            layer.setScrollable(scrollableCheckBox.Checked);
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void zDepthTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                zDepth = double.Parse(zDepthTextBox.Text);

                if (zDepth < 0)
                {
                    zDepth = 0;
                    zDepthTextBox.Text = "" + zDepth;
                }
            }
            catch (FormatException)
            {
                string inputPattern = "^\\d*,?\\d*$";

                if (!Regex.IsMatch(zDepthTextBox.Text, inputPattern)) zDepthTextBox.Text = "" + zDepth;
            }
        }

        private void zDepthTextBox_Leave(object sender, EventArgs e)
        {
            zDepthTextBox.Text = "" + zDepth;
        }

        private void LayerPreferencesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            levelEditorForm.SetDialogFormOpen(false);
        }
    }
}
